<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="工厂" prop="factory">
        <DeptSelect
          v-model="queryParams.factory"
          placeholder="请选择工厂"
          style="width: 240px"
          type="factory"
        ></DeptSelect>
      </el-form-item>
      <el-form-item label="调拨单编号" prop="moveCode">
        <el-input
          v-model="queryParams.moveCode"
          placeholder="请输入调拨单编号"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调拨单名称" prop="moveName">
        <el-input
          v-model="queryParams.moveName"
          placeholder="请输入调拨单名称"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调拨单时间" prop="time">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="dict in wh_move_inventory_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['warehouse:movehouse:add']"
        >
          内部调拨
        </el-button>
      </el-col>

      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="moveInventoryList"
      border
      maxHeight="500"
      @selection-change="handleSelectionChange"
      show-overflow-tooltip
    >
      <el-table-column label="调拨单编号" align="center" prop="moveCode">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">
            {{ scope.row.moveCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="调拨单名称" align="center" prop="moveName" />
      <el-table-column label="调拨原因" align="center" prop="moveReason" />
      <el-table-column label="调拨时间" align="center" prop="moveTime"></el-table-column>
      <el-table-column label="工厂" align="center" prop="factoryName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wh_move_inventory_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        minWidth="250"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.status === 'WAIT_MOVE'"
            link
            icon="finished"
            type="primary"
            @click="confirmInStore(scope.row)"
            v-hasPermi="['warehouse:movehouse:confirm']"
          >
            确认调拨
          </el-button>
          <el-button
            v-if="scope.row.status === 'WAIT_MOVE'"
            link
            icon="edit"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:movehouse:edit']"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.row.status === 'WAIT_MOVE'"
            link
            icon="delete"
            type="primary"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:movehouse:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMoveInventory, delMoveInventory } from "@/api/warehouse/moveInventory";

import warehouse from "./warehouse.vue";

export default {
  name: "MoveInventory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dateRange: [],
      // 子表选中数据
      checkedWhStorehouseInDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料调拨表格数据
      moveInventoryList: [],
      // 物料调拨明细表格数据
      whStorehouseInDetailList: [],
      selectData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inOrderType: null,
        moveCode: null,
        moveName: null,
        erpOrderCode: null,
        factoryCode: null,
        workshopCode: null,
        lineCode: null,
        sectionCode: null,
        storehouseCode: null,
        deliveryPerson: null,
        receiver: null,
        comeTime: null,
        inTime: null,
        status: null,
      },
      wh_move_inventory_status: [],
    };
  },
  created() {
    const { wh_move_inventory_status } = this.useDict("wh_move_inventory_status");
    this.wh_move_inventory_status = wh_move_inventory_status;
    this.getList();
  },
  methods: {
    /** 查询物料调拨列表 */
    getList() {
      this.loading = true;
      listMoveInventory(this.addDateTimeRange(this.queryParams, this.dateRange))
        .then((response) => {
          this.moveInventoryList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectData = selection;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.subPage(warehouse, { title: "添加调拨" }, () => {
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.subPage(warehouse, { title: "编辑调拨", data: row }, () => {
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      this.subPage(warehouse, { title: "调拨详情", data: row }, () => {
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (row.id) {
        if (row.status !== "WAIT_MOVE") {
          this.$modal.msgError("仅可删除待调拨数据");
          return;
        }
      } else {
        let canDel = this.selectData.filter((el) => el.status !== "WAIT_MOVE");
        if (canDel.length) {
          this.$modal.msgError("仅可删除待调拨数据");
          return;
        }
      }
      const codes =
        row.moveCode ||
        ids.map((id) => this.moveInventoryList.find((item) => item.id === id)?.moveCode).join(", ");
      this.$modal
        .confirm('是否确认删除编号为"' + codes + '"的数据项？')
        .then(function () {
          return delMoveInventory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    confirmInStore(row) {
      if (row) {
        this.subPage(warehouse, { title: "确认调拨", data: row }, () => {
          this.getList();
        });
      } else {
        let canDel = true;
        let lineStr = "";
        this.selectData.forEach((el) => {
          if (!lineStr) {
            lineStr = `${el.factoryCode}-${el.workshopCode}-${el.lineCode}`;
          } else {
            if (lineStr !== `${el.factoryCode}-${el.workshopCode}-${el.lineCode}`) {
              canDel = false;
            }
          }
        });
        if (!canDel) {
          this.$modal.msgError("请选择统一产线数据");
          return;
        }
        this.subPage(
          warehouse,
          { title: "确认调拨", data: this.selectData, code: this.ids },
          () => {
            this.getList();
          }
        );
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "warehouse/moveInventory/export",
        {
          ...this.queryParams,
        },
        `moveInventory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
